#coding: utf-8
def _():
    pass
from base import StringBuffer
def render(pi, maxpage, urlgen, ctx):
    if maxpage == 1:
        return ''
    _ = StringBuffer()
    pstar = max(pi - 4,1)
    pend  = min(pi + 4,maxpage)
    
    if pi - pstar < 4:
        pend = pend + (4 - (pi - pstar))
    if pend - pi < 4:
        pstar = pstar - (4 - (pend - pi))
    pstar = max(pstar, 1)
    pend  = min(pend, maxpage)
       
    bs = False
    be = False
    if pi - 1>= 10:
        bs = True
    else:
        pstar = 1
    if maxpage - pi >= 10:
        be = True
    else:
        pend = maxpage
    
    _(u'''<div class='pagenavi'>''')
    if pi == 1:
        _(u'''<span style='display:none'>上一页</span>''')
    else:
        _(u'''<span><a href='%s'>上一页</a></span>''' % urlgen(ctx,(pi - 1)))
    if bs:
        _(u'''<span><a href='%s'>%d</a></span> ''' % (urlgen(ctx, 1), 1))
        _(u'''<span><a href='%s'>%d</a></span>'''  % (urlgen(ctx, 2), 2))
        _(u'''...''')
    for i in range(pstar, pend + 1):
        if i == pi:
            _(u'''<span class='pagenow'>%d</span> ''' % (i))
        else:
            _(u'''<span><a href='%s'>%d</a></span> ''' % (urlgen(ctx, i), i))
    if be:
        _(u'''...''')
        _(u'''<span><a href='%s'>%d</a></span> ''' % (urlgen(ctx,maxpage-1), maxpage-1))
        _(u'''<span><a href='%s'>%d</a></span>''' % (urlgen(ctx, maxpage), maxpage))
    if pi >= maxpage:
        _(u'''<span style='display:none'>下一页</span>''')
    else:
        _(u'''<span><a href='%s'>下一页</a></span>''' % urlgen(ctx, (pi + 1)))
    _(u'''</div>''')
    return _.getvalue()